﻿@{
    Layout = "~/Areas/Admin/Views/Shared/_LayoutAdmin.cshtml";
}
@model FsCms.Entity.SysRole
<div style="background:#fff; padding:10px 5px 0px 5px; " id="index_SysRoleDataList">
    <div class="dataGrid_Toolbar">
        <form class="layui-form" action="">
            <div class="layui-form-item fs-mb-0">
                <div class="layui-inline" style="float: left;">
                    <div class="layui-input-inline" style="width: auto;">
                        <button class="layui-btn layui-btn-normal layui-btn-sm" type="button" data-type="add"><i class="layui-icon">&#xe61f;</i>新增</button>
                        <button class="layui-btn layui-btn-normal layui-btn-sm" type="button" data-type="update"><i class="layui-icon">&#xe642;</i>修改</button>
                        <button class="layui-btn layui-btn-normal layui-btn-sm" type="button" data-type="delete"><i class="layui-icon">&#xe640;</i>删除</button>
                        <button class="layui-btn layui-btn-normal layui-btn-sm" type="button" data-type="export"><i class="layui-icon">&#xe630;</i>导出</button>

                        <button class="layui-btn layui-btn-normal layui-btn-sm" type="button" data-type="setPerview"><i class="layui-icon">&#xe630;</i>授权</button>
                    </div>
                </div>

                <div class="layui-inline" style="float: right;">
                    <div class="layui-input-inline" style="width: auto;">
                        <input class="layui-input fs-search-txt" name="id" id="search_txt" placeholder="角色编码/角色名称" autocomplete="off">
                    </div>
                    <div class="layui-input-inline" style="width: auto;">
                        <button class="layui-btn layui-btn-sm" type="button" data-type="reload"><i class="layui-icon">&#xe615;</i>搜索</button>
                    </div>
                </div>
            </div>
        </form>
    </div>

    <table class="layui-hide" id="SysRoleDataList" lay-filter="SysRoleDataList"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="setPerview">授权</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="SysRoleEdit">
    </script>
</div>

<script>
    var index_SysRoleDataList = $("#index_SysRoleDataList")
    var bodyheight = 0;
    $(function () {
        bodyheight = $(window).height();// document.body.scrollHeight;
    });

    layui.use(['form', 'treeTable', 'table', 'jquery'], function () {
        var table = layui.table;
        var treeTable = layui.treeTable;
        //var authtree = layui.authtree;
        var form = layui.form
        var $ = layui.$;
        bodyheight = $(window).height();
        var datagrid = freejs.dataGrid(table, form, {
            //freejs封装参数
            controller: "/Admin/SysRole",
            pageElem: index_SysRoleDataList,
            elem: '#SysRoleDataList',
            formElem: "SysRoleEdit",
            formArea: ['1100px', '660px'],
            tableOptions: {
                //table固定参数
                where: function () {
                    var seniorQueryJson = {};
                    seniorQueryJson = JSON.stringify(seniorQueryJson);
                    var query = { searchContent: $("input[type=search_txt]", index_SysRoleDataList).val(), seniorQueryJson: seniorQueryJson };
                    return query;
                },
                cols: [[
                    { checkbox: true, fixed: true }
                    , { field: '@nameof(Model.Id)', title: 'Id', width: 80, fixed: 'left', unresize: true, sort: true }
                    , { field: '@nameof(Model.RoleCode)', title: '角色编码', width: 120 }
                    , { field: '@nameof(Model.RoleName)', title: '角色名称', width: 160, sort: true }
                    , { field: '@nameof(Model.Description)', title: '描述' }
                    , { field: '@nameof(Model.Status)', title: '状态', width: 80, sort: true }
                    , { field: '@nameof(Model.CreateDt)', title: '创建时间', width: 160 }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 180 }
                ]],
                page: true,
                height: bodyheight - 212,
            },
            editBefore: function (_form, data) {
                $.ajax({
                    url: '/Admin/SysMenu/GetTree',
                    data: { id: data.Id },
                    type: "GET",
                    dataType: 'json',
                    success: function (data) {
                        console.log(data);
                        var re = treeTable.render({
                            elem: '#tree-table',
                            data: data,
                            icon_key: 'title',
                            is_checkbox: true,
                            //checked: {
                            //    key: 'id',
                            //    data: [0, 1, 4, 10, 11, 5, 2, 6, 7, 3, 8, 9],
                            //},
                            end: function (e) {
                                form.render();
                            },
                            cols: [
                                {
                                    key: 'title',
                                    title: '菜单名称',
                                    width: '100px',
                                    template: function (item) {
                                        if (item.level == 0) {
                                            return '<span style="color:red;">' + item.title + '</span>';
                                        } else if (item.level == 1) {
                                            return '<span style="color:green;">' + item.title + '</span>';
                                        } else if (item.level == 2) {
                                            return '<span style="color:#aaa;">' + item.title + '</span>';
                                        }
                                    }
                                },
                                {
                                    title: '按钮',
                                    align: 'left',
                                    template: function (item) {
                                        var btn_html = "";
                                        $.each(item.buttons, function (i, btn) {
                                            btn_html += '<input type="checkbox" name="' + btn.ButtonCode+'" checked="" title="' + btn.ButtonName+'">';
                                            //btn_html += '<input type="checkbox" name="close" lay-skin="switch" lay-text="ON|OFF">';
                                        });
                                        return btn_html;
                                    }
                                }
                            ]
                        });
                    }
                });
                
                //$.ajax({
                //    url: '/Admin/SysMenu/GetTree',
                //    data: { id: data.Id },
                //    type: "GET",
                //    dataType: 'json',
                //    success: function (data) {
                //        var checkdatas = data.where(function (w) { return w.ischecked == true; }).select(function (s) { return s.id });
                //        var trees = authtree.listConvert(data, {
                //            primaryKey: 'id'
                //            , startPid: 0
                //            , parentKey: 'pid'
                //            , nameKey: 'name'
                //            , valueKey: 'id',
                //            checkedKey: checkdatas
                //        });
                //        // 如果后台返回的不是树结构，请使用 authtree.listConvert 转换
                //        authtree.render('#LAY-auth-tree-index', trees, {
                //            inputname: 'authids[]',
                //            layfilter: 'lay-check-auth',
                //            autowidth: true,
                //        });
                //    }
                //});
            },
            submitBefore: function (data) {
                //var authids = authtree.getAll('#LAY-auth-tree-index');
                //console.log('Choosed authids is', authids);
                //obj.field.authids = authids;
                return data;
            },
            rowTool: {
                setPerview: function (obj, layEvent) {

                }
            }
        });

        //扩展按钮
        datagrid.active.setPerview = function () {
            alert("授权");
        };
    });
</script>
